package com.ohuang.jiaxiaotong.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.myhttp.CallBackString;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.EventMsgCode;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.Util.Sys;
import com.ohuang.jiaxiaotong.base.BaseAc;
import com.ohuang.jiaxiaotong.entity.EventMsg;
import com.ohuang.jiaxiaotong.entity.User;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class UploadNoticeActivity extends BaseAc {
    private Button button_upload;
    private EditText editText;
    private String className;
    private String content;
    private static final int SI_SUCCESS = 0, SI_FAIL = 1, SI_ERROR = 2, SI_OTHER = 3;
    private Dialog dialogLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_notice);
        setTitle("发布公告");
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        className = intent.getStringExtra("className");
        Ohuang.changeStatusBarTextImgColor(this, true);
        Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.mipmap.icon_goback);//设置ActionBar返回按钮
        button_upload = findViewById(R.id.btn_notice_UploadNotice);
        editText = findViewById(R.id.edit_notice_UploadNotice);

        initView();
    }

    private void initView() {
        button_upload.setOnClickListener(v -> {
            if (Sys.isClick()) {
                content = editText.getText().toString();
                if (content.isEmpty()) {
                    Toast.makeText(UploadNoticeActivity.this, "公告不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                dialogLoading = Ohuang.dialogLoadingshow(UploadNoticeActivity.this, "正在发布");
                HttpUploadNotice();
            }
        });
    }

    private void HttpUploadNotice() {
        HashMap<String,String> map=new HashMap<>();
         map.put("className", className);
         map.put("content", content);

        Ihttp.getInstance().post(MyHttpURL.mainUrl + "teacher/fillNotice", map, new CallBackString() {
            @Override
            public void fail(String s) {
                myHandler.sendEmptyMessage(SI_ERROR);
            }

            @Override
            public void success(String ojb) {
                if ("success".equals(ojb)) {
                    myHandler.sendEmptyMessage(SI_SUCCESS);
                } else if ("error".equals(ojb)) {
                    myHandler.sendEmptyMessage(SI_FAIL);
                } else {
                    myHandler.sendEmptyMessage(SI_OTHER);
                }
            }
        });

    }

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case SI_SUCCESS:
                    dialogLoading.dismiss();
                    EventBus.getDefault().post(new EventMsg(EventMsgCode.CODE_4, "通知NoticeFragment"));
                    Ohuang.dialogshow(UploadNoticeActivity.this, "发布成功", "公告发布成功", v -> {
                        finish();
                    });
                    break;
                default:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(UploadNoticeActivity.this, "发布失败", "网络错误");
                    break;

            }
        }
    };

    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {//左上角返回按钮
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void GetUserEventBus(User user) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
